<?php

/**
 *
 * @file   GroupController.php
 * @date   2016-9-1 15:11:41
 * @author Zhenxun Du<5552123@qq.com>
 * @version    SVN:$Id:$
 */

namespace app\admin\controller;

class Post extends Common {

    /**
     * 发布管理
     * @return [type] [description]
     */
    public function index() {
        $keyword=input('keyword');
        $select=input('type');
        //var_dump($select);
        if (!empty($keyword)) {
            $map['title']  = ['like','%'.$keyword.'%'];
            $lists=db('post')->alias('p')->join('category c','p.category = c.id','LEFT')->where($map)->order('p.id desc')->field('p.*,c.name')->paginate(10);
            //dump($lists);exit;

        }else if (!empty($select)) {
                $map['name']  = $select;
                $lists=db('post')->alias('p')->join('category c','p.category = c.id','LEFT')->where($map)->order('p.id desc')->field('p.*,c.name')->paginate(10);
                //dump($lists);exit;

        }else{
            $where = array('category.status'=>1);
            $lists=db('post')->alias('p')->join('category c','p.category = c.id','LEFT')->where($where)->order('p.sort desc','p.id desc')->field('p.*,c.name')->paginate(10);
            //dump($lists);exit;
        }

        //分类下拉列表
        $list1=db("category")->select();

        //dump($list1);exit;

        $this->assign('data1', $list1);
        $this->assign('data', $lists);
        return $this->fetch();

    }
    /**
     * 更改状态
     * @return [type] [description]
     */
    public function del() {
        $id = input('id');
        $status = -1;
        $save = array('id'=>$id,'status'=>$status);
        $val = db('post')->update($save);
        if($val){
             $this->success('删除成功');
        }else{
             $this->error('删除失败');
        }
    }

    public function add() {
        $id = input('id');
        if(request()->isPost()){
            $data['category'] = input('category');
            $data['title'] = input('title');
            $data['content'] = input('editorValue');
            $data['create_time'] = time();
            $data['mark'] = input('mark');
            $data['sort'] = input('sort');
            $data['link'] = input('link');
            $data['uid'] = 7;//默认发布的人
            $data['type'] = 1;
           // dump($_POST);exit;
            if(!empty($id)){
                $data['id'] = $id;
                $val = db('post')->update($data);
            }else{
                $val = db('post')->insert($data);
            }
            if($val){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }else{

            $val = db('post')->where('id',$id)->find();
            $this->assign('info',$val);
        }
        $cate = db('category')->where('status',1)->select();
        $this->assign('data',$cate);
        return $this->fetch();


    }



}
